package com.lgr.store.utils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

/*
 * 处理整个系统中日期类型的转换
 * 1.实现Converter接口
 * 2.实现抽象方法convert，进行处理
 * 3.要在spring-web.xml文件里面配置该类，为整个系统服务
 * <bean id="dateUtil" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" >
        <property name="converters" >
            <list>
                <bean class="com.ht.base.DateUtil"></bean>
            </list>
        </property>
    </bean>
    4.在spring-web.xml里面配置如下代码
    <mvc:annotation-driven conversion-service="dateUtil"></mvc:annotation-driven>
 * */
public class DateUtil  implements Converter<String, Date> {

    @Override
    public Date convert(String source) {

        System.out.println("source="+source);
        //实现将字符串转成日期类型(格式是yyyy-MM-dd HH:mm:ss)
        SimpleDateFormat dateFormat = null;
        if(!StringUtils.isEmpty(source)){
            //判断日期字符串是否包含了时间部分
            int pos1 = source.indexOf(":");//从左往右查找字符串中第一个冒号的位置
            int pos2 = source.lastIndexOf(":");//从右往左查找字符串中第一个冒号的位置
            source = source.replace("T", " ");//把字符串中的T替换为空格
            if(pos1>0 && pos1 != pos2){
                dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }else if(pos1>0 && pos1==pos2){
                source = source +":00";
                dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }else{
                dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            }
        }
        try {
            return dateFormat.parse(source);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //如果参数绑定失败返回null
        return null;
    }
    

	/**
	 * 得到本周周一
	 * 
	 * @return yyyy-MM-dd
	 */
	public static String getMondayOfThisWeek() {
		Calendar c = Calendar.getInstance();
		int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayofweek == 0)
			dayofweek = 7;
		c.add(Calendar.DATE, -dayofweek + 1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(c.getTime())+" 00:00:00";
	}

	/**
	 * 得到本周周日
	 * 
	 * @return yyyy-MM-dd
	 */
	public static String getSundayOfThisWeek() {
		Calendar c = Calendar.getInstance();
		int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayofweek == 0)
			dayofweek = 7;
		c.add(Calendar.DATE, -dayofweek + 7);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(c.getTime())+" 23:59:59";
	}
	
	/**
	 * 得到本月最后一天
	 * 
	 * @return
	 */
	public static String getLastDateOfMonth() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, days);
		String result = format.format(cal.getTime())+" 23:59:59";
		return result;
	}

	/**
	 * 得到本月第一天
	 * 
	 * @return
	 */
	public static String getFristDateOfMonth() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int days = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, days);
		String result = format.format(cal.getTime())+" 0:00:00";
		return result;
	}
	
	

	/**
	 * 得到上月第一天
	 * 
	 * @return
	 */
	public static String getFristDateOfPreMonth() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.MONTH, -1);//设置为上一个月
		cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号
		String result = format.format(cal.getTime())+" 0:00:00";
		return result;
	}
	
	/**
	 * 得到上月最后一天
	 * 
	 * @return
	 */
	public static String getLastDateOfPreMonth() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());//设置为当前时间
		cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号
		cal.add(Calendar.DAY_OF_MONTH, -1);//减去一天 变为上个月最后天
		String result = format.format(cal.getTime())+" 23:59:59";
		return result;
	}
	
	public static void main(String[] args) {
		System.out.println(DateUtil.getFristDateOfPreMonth());
		System.out.println(DateUtil.getLastDateOfPreMonth());
	}

}
